#include "InterpolationSolver.h"
#include <iostream>
#include <fstream>

//[题目E要求]It is suspected that the high amounts of tannin in mature oak leaves inhibit the growth of the winter moth larvae that extensively damage these trees in certain years. The following table lists the average weight of two samples of larvae at times in the first 28 days after birth. The first sample was reared on young oak leaves, whereas the second sample was reared on mature leaves from the same tree.
//(a)Use Newton’s formula to approximate the average weight curve for each sample.
//(b)Predict whether the two samples of larvae will die after another 15 days.

class Fun1 : public Function
{
public:
    double operator()(double _x)
    {
        double y;
        switch ((int)_x)
        {
            case 0:
                y = 6.67;
                break;
            case 6:
                y = 17.3;
                break;
            case 10:
                y = 42.7;
                break;
            case 13:
                y = 37.3;
                break;
            case 17:
                y = 30.1;
                break;
            case 20:
                y = 29.3;
                break;
            case 28:
                y = 28.7;
                break;
            default:
                break;
        }
        return y;
    }
};

class Fun2 : public Function
{
public:
    double operator()(double _x)
    {
        double y;
        switch ((int)_x)
        {
            case 0:
                y = 6.67;
                break;
            case 6:
                y = 16.1;
                break;
            case 10:
                y = 18.9;
                break;
            case 13:
                y = 15.0;
                break;
            case 17:
                y = 10.6;
                break;
            case 20:
                y = 9.44;
                break;
            case 28:
                y = 8.89;
                break;
            default:
                break;
        }
        return y;
    }
};

int main()
{
    std::ofstream fout;
    fout.open("proE.txt");
    Fun1 Sp1;
    Fun2 Sp2;
    
    std::vector<double> v1{0,6,10,13,17,20,28};
    std::vector<double> v2{0,6,10,13,17,20,28};

    Newton_Formula b1(Sp1, v1);
    Newton_Formula b2(Sp2, v2);
    std::vector<double> Coef1,Coef2;

    Coef1 = b1.Get_coef();
    Coef2 = b2.Get_coef();
    std::cout << "The Coefficients of curve for Sp1 are: ";
    for (int i = 0; i < v1.size(); i++)
    {
        std::cout << Coef1[i] << " ";
    }

    std::cout << "" << std::endl;
    std::cout << "The Coefficients of curve for Sp2 are: ";

    for (int i = 0; i < v1.size(); i++)
    {
        std::cout << Coef2[i] << " ";
    }

    std::cout << "" << std::endl;
    std::cout << "Average weight of Sp1 after another 15 days is: " << b1.solve(43.0) << std::endl;
    std::cout << "Average weight of Sp2 after another 15 days is: " << b2.solve(43.0) << std::endl;

    fout << "x=0:0.01:28;" << std::endl;
    fout << "y=" << "[";
    for (int i = 0; i < 2800; i++)
    {
        fout << b1.solve(0.01*i) << ", ";
    }
    fout << b1.solve(28.0) << "];" << std::endl;
    fout << "plot(x,y,'*');" << std::endl;

    fout << "x=0:0.01:28;" << std::endl;
    fout << "y=" << "[";
    for (int i = 0; i < 2800; i++)
    {
        fout << b2.solve(0.01*i) << ", ";
    }
    fout << b2.solve(28.0) << "];" << std::endl;
    fout << "plot(x,y,'*');" << std::endl;
    
    fout.close();
    return 0;
}
